package com.itheima.advantagesup.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.itheima.advantagesup.common.PageBean;
import com.itheima.advantagesup.common.constant.CateDefaultDate;
import com.itheima.advantagesup.dto.AddArticleDTO;
import com.itheima.advantagesup.dto.ArticlePageDTO;
import com.itheima.advantagesup.entity.YddArticle;
import com.itheima.advantagesup.entity.YddUser;
import com.itheima.advantagesup.mapper.YddArticleMapper;
import com.itheima.advantagesup.service.YddArticleService;
import com.itheima.advantagesup.vo.AddArticleVO;
import com.itheima.advantagesup.vo.YddArticleAddVO;
import com.itheima.advantagesup.vo.YddArticleUpdateVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;
import java.time.LocalDateTime;
import java.util.List;

/**
 * @author Du leilei
 */
@Service
public class YddArticleServiceImpl implements YddArticleService {

    @Autowired
    private YddArticleMapper yddArticleMapper;
    @Autowired
    private HttpServletRequest httpServletRequest;

    @Override
    public AddArticleVO pageQuery(ArticlePageDTO articlePageDTO) {
        PageHelper.startPage((Integer.parseInt(articlePageDTO.getPage())), Integer.parseInt(articlePageDTO.getLimit()));

        YddArticle yddArticle = new YddArticle();
        BeanUtils.copyProperties(articlePageDTO, yddArticle);
        Page<YddArticle> yddArticles = yddArticleMapper.pageQuery(yddArticle);
        AddArticleVO addArticleVO = AddArticleVO.builder()
                .count((int) yddArticles.getTotal())
                .totalPages(yddArticles.getPages())
                .pageSize(yddArticles.getPageSize())
                .currentPage(yddArticles.getPageNum())
                .build();
        List<YddArticle> result = yddArticles.getResult();
        addArticleVO.setData(result);
        return addArticleVO;
    }

    @Override
    public YddArticleAddVO add(AddArticleDTO addArticleDTO) {
        YddArticle yddArticle = new YddArticle();

        BeanUtils.copyProperties(addArticleDTO, yddArticle);
        if (addArticleDTO.getUserId() == null) {
            addArticleDTO.setUserId(0);
        }

        if (addArticleDTO.getStatus() == null) {
            addArticleDTO.setStatus("2");
        }

        BeanUtils.copyProperties(addArticleDTO, yddArticle);
        yddArticle.setCreate_date(System.currentTimeMillis());
        yddArticle.setUpdate_date(System.currentTimeMillis());
        yddArticle.setDefaultData(CateDefaultDate.DEFAULT_VALUE);
        yddArticle.setStatus("2");
        yddArticleMapper.insert(yddArticle);
        YddArticleAddVO yddArticleAddVO = new YddArticleAddVO();
        BeanUtils.copyProperties(addArticleDTO, yddArticleAddVO);
        return yddArticleAddVO;
    }

    @Override
    public void delArticle(Integer id) {
        yddArticleMapper.delArticle(id);
    }

    @Override
    public YddArticle queryArticle(Integer id) {
        YddArticle yddArticle = yddArticleMapper.selectArticle(id);
        return yddArticle;
    }

    @Override
    public YddArticleUpdateVO updateArticle(YddArticle yddArticle) {



        YddArticleUpdateVO yddArticleUpdateVO = new YddArticleUpdateVO();
        BeanUtils.copyProperties(yddArticle, yddArticleUpdateVO);

        yddArticleMapper.updateArticle(yddArticle);
        return yddArticleUpdateVO;
    }
}
